﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;

public partial class _Default : System.Web.UI.Page
{
	private SqlConnection _connection = new SqlConnection();

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!Page.IsPostBack)
		{
			BindData();
		}
	}

	private void BindData()
	{
		string strConnectionString = "server=127.0.0.1;uid=sa;pwd=liuxiaozeng;database=NorthwindCHS";
		_connection.ConnectionString = strConnectionString;

		SqlDataAdapter dataAdapter = new SqlDataAdapter(@"SELECT 产品ID, 类别名称,产品名称,产品.类别ID,单价,库存量
														FROM 产品,类别
														WHERE 产品.类别ID=类别.类别ID		
															", _connection);
		DataSet ds = new DataSet();
		dataAdapter.Fill(ds, "Product");

		gdvProduct.DataSource = ds.Tables["Product"];
		gdvProduct.DataBind();
	}

	private void ShowProduct(string productId)
	{
		string strConnectionString = "server=127.0.0.1;uid=sa;pwd=liuxiaozeng;database=NorthwindCHS";
		_connection.ConnectionString = strConnectionString;

		SqlDataAdapter dataAdapter = new SqlDataAdapter(@"SELECT 产品名称,单价,库存量
														FROM 产品
														WHERE 产品ID=" + productId, _connection);
		DataSet ds = new DataSet();
		dataAdapter.Fill(ds, "Product");

		TextBox1.Text = ds.Tables["Product"].Rows[0]["产品名称"].ToString();
		TextBox2.Text = ds.Tables["Product"].Rows[0]["单价"].ToString();
		TextBox3.Text = ds.Tables["Product"].Rows[0]["库存量"].ToString();


	}

	protected void gdvProduct_DataBinding(object sender, EventArgs e)
	{

	}
	protected void gdvProduct_DataBound(object sender, EventArgs e)
	{

	}
	protected void gdvProduct_SelectedIndexChanged(object sender, EventArgs e)
	{
		for (int i = 0; i < gdvProduct.Rows.Count; i++)
		{
			if (i == gdvProduct.SelectedIndex)
			{
				gdvProduct.Rows[i].BackColor = Color.LightPink;
			}
			else
			{
				gdvProduct.Rows[i].BackColor = Color.Transparent;
			}
		}

		Session["productid"] = gdvProduct.Rows[gdvProduct.SelectedIndex].Cells[0].Text;
	}
	protected void gdvProduct_RowEditing(object sender, GridViewEditEventArgs e)
	{
		ShowProduct(gdvProduct.Rows[e.NewEditIndex].Cells[0].Text);
		gdvProduct.EditIndex = e.NewEditIndex;

		Session["productid"] = gdvProduct.Rows[e.NewEditIndex].Cells[0].Text;

		//ShowProduct(gdvProduct.DataKeys[e.NewEditIndex].ToString());
		BindData();

	}
	protected void gdvProduct_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
	{
		gdvProduct.EditIndex = -1;
		BindData();
	}
	protected void gdvProduct_RowUpdating(object sender, GridViewUpdateEventArgs e)
	{
		string newPrice = "";


		string productId = Session["productid"].ToString();

		string strConnectionString = "server=127.0.0.1;uid=sa;pwd=liuxiaozeng;database=NorthwindCHS";
		_connection.ConnectionString = strConnectionString;

		SqlCommand command = new SqlCommand("UPDATE 产品 SET 产品名称='" + TextBox1.Text
											+ "', 单价=" + TextBox2.Text
											+ ",库存量=" + TextBox3.Text + " WHERE 产品ID=" + productId, _connection);

		if(_connection.State != ConnectionState.Open)
			_connection.Open();

		command.ExecuteNonQuery();

		_connection.Close();
	}
	protected void Button1_Click(object sender, EventArgs e)
	{
		string productId = Session["productid"].ToString();

		string strConnectionString = "server=127.0.0.1;uid=sa;pwd=liuxiaozeng;database=NorthwindCHS";
		_connection.ConnectionString = strConnectionString;

		SqlCommand command = new SqlCommand("DELETE FROM 产品 WHERE 产品ID=" + productId, _connection);

		if (_connection.State != ConnectionState.Open)
			_connection.Open();

		command.ExecuteNonQuery();

		_connection.Close();

		BindData();
	}
}
